package com.linhh.mall.center.common.util;

import com.linhh.mall.center.common.constant.ConstantKey;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @Author linhh
 * @Date 2022 10 19 14 21
 **/
public class JwtUtil {
    /**
     * 生成token
     * @param subject
     * @param issueDate
     * @param expireDate
     * @return
     */
    public static String createToken(String subject, Date issueDate, Date expireDate){
        String token =  Jwts.builder()
                .setSubject(subject)
                .setIssuedAt(issueDate)  //签发时间
                .setExpiration(expireDate) //过期时间
                .signWith(SignatureAlgorithm.HS256, ConstantKey.SIGNING_KEY)
                .compact();
        return token;
    }


    /**
     * 解析Token
     * @param token
     * @return
     */
    public static Claims parseToken(String token){
        return Jwts.parser()
                .setSigningKey(ConstantKey.SIGNING_KEY)
                .parseClaimsJws(token.replace("Bearer ", ""))
                .getBody();
    }
}
